package org.feng.service.impl;

import org.feng.datasource.DataSourcePool;
import org.feng.datasource.DataSourceProperties;
import org.feng.datasource.DynamicDataSource;
import org.feng.datasource.DynamicDataSourceContextHolder;
import org.feng.service.DynamicDataSourceService;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.util.Map;

/**
 * 动态数据源服务
 *
 * @author pine
 * @version v1.0
 * @since 2025-06-07 18:23
 */
@Service
public class DynamicDataSourceServiceImpl implements DynamicDataSourceService {

    @Autowired
    private DataSource dynamicDataSource;


    /**
     * 获取当前支持的所有数据源信息
     */
    @Override
    @Tool(name = "getAllDataSourceInfo", description = "获取当前支持的所有数据源信息")
    public Map<String, DataSourceProperties> getAllDataSourceInfo() {
        return DataSourcePool.getDatasourcePropertiesPool();
    }

    /**
     * 根据租户获取指定的数据源信息
     *
     * @param tenantId 租户
     */
    @Override
    @Tool(name = "getDataSourceInfo", description = "根据租户获取指定的数据源信息")
    public DataSourceProperties getDataSourceInfo(@ToolParam(description = "租户") String tenantId) {
        return DataSourcePool.getDatasourceProperties(tenantId);
    }

    /**
     * 根据租户切换数据源
     *
     * @param tenantId 租户
     */
    @Override
    @Tool(name = "switchDataSource", description = "根据租户切换数据源")
    public void switchDataSource(@ToolParam(description = "租户") String tenantId) {
        DynamicDataSourceContextHolder.setDataSourceKey(tenantId);
    }

    /**
     * 根据租户移除数据源
     *
     * @param tenantId 租户
     */
    @Override
    @Tool(name = "removeDataSource", description = "根据租户移除数据源")
    public void removeDataSource(@ToolParam(description = "租户") String tenantId) {
        DynamicDataSourceContextHolder.removeDataSourceKey();
        DataSourcePool.removeDatasourceProperties(tenantId);
        DataSourcePool.removeDataSource(tenantId);
    }

    /**
     * 清空所有数据源
     */
    @Override
    @Tool(name = "clearDataSource", description = "清除所有数据源")
    public void clearDataSource() {
        DynamicDataSource dynamicDataSource = (DynamicDataSource) this.dynamicDataSource;
        dynamicDataSource.clearResolvedDataSources();
        DataSourcePool.getDatasourcePropertiesPool().clear();
        DataSourcePool.getDataSourcePool().clear();
        DynamicDataSourceContextHolder.removeDataSourceKey();
    }


    /**
     * 新增或更新数据源，并切换到刚刚操作的数据源上
     *
     * @param tenantId             租户
     * @param dataSourceProperties 数据源配置
     */
    @Override
    @Tool(name = "addOrUpdateDataSource", description = "新增或更新数据源，并切换到刚刚操作的数据源上")
    public void addOrUpdateDataSource(@ToolParam(description = "租户") String tenantId, @ToolParam(description = "数据源连接信息") DataSourceProperties dataSourceProperties) {
        DataSourcePool.setDatasourceProperties(tenantId, dataSourceProperties);
        DataSourcePool.setDataSource(tenantId, dataSourceProperties);
        DynamicDataSourceContextHolder.setDataSourceKey(tenantId);
    }
}
